
// #ifdef MP-WEIXIN
import { getSessionCode } from '@/common/wechat';
// #endif



export default {
	state: {
		platform:0,
		hasLogin: false,
		loginProvider: "",
		code:"",
		openid: null,
		userToken:"",
		weixinUserKey:"",
		appUserInfo:null,
		memberInfo:null,
		memberOperate:null,
		intervals:[],
		orderInfo:{}
	},
	mutations: {

		setProvider(state, provider){
			// #ifdef MP-WEIXIN
			state.platform = 2;
			// #endif
			state.loginProvider = provider;
		},
		
		setMemberInfo(state, memberInfo){
			state.memberInfo = memberInfo;
		},
		
		setMemberOperate(state, memberOperate){
			state.memberOperate = memberOperate;
		},
		
		setCode(state, code){
			state.code = code;
		},
		
		setIntervals(state, intervals){
			state.intervals = intervals;
		},
		
		
		setWeixinUserKey(state, weixinUserKey){
			state.weixinUserKey = weixinUserKey;
		},
		
		setUserToken(state, userToken){
			state.userToken = userToken;
		},
		
		setAppUserInfo(state, appUserInfo){
			state.appUserInfo = appUserInfo;
		},

		setOrderInfo(state, orderInfo){
			state.orderInfo = orderInfo;
		},
		
		logout(state) {
			state.hasLogin = false
			state.openid = null
		},
		setOpenid(state, openid) {
			state.openid = openid
		},
		
       
	},
    getters:{
       
    },
	actions: {

		//获取code
		getCode: async function ({commit,state}) {
			return await new Promise((resolve, reject) => {	
				uni.login({
					provider: state.loginProvider,
					success: (res) => {
						console.log("code:"+res.code);
						commit('setCode',res.code);
						resolve(res.code);
					},
					fail: (err) => {
						console.log('授权登录失败：' + JSON.stringify(err));
						reject(err);
					}
				});
			});
		},
		
		getAppToken: async function ({commit,state}) {
	
			// #ifdef MP-WEIXIN
			let keyInfo=await getSessionCode({code:state.code});
			
			if(keyInfo[1].data){
				let userToken = keyInfo[1].data.openid;
				
				if(process.env.CUR_ENV=="local"){
					// userToken =  "oOd-Q4rNiwtk45fpswNisKml0HN0"; //师瑶
					userToken =  "Q4v540I4rWGgeRoeejkKELSA"; //宋方针
					
					//userToken =  "oOd-Q4iOks3mNm8l8rYFcOiao6k4";//徐麟
					//userToken="oOd-Q4qwqCvB2Ylc1mkZvD0vewec";//黄少素
					//userToken =  "oOd-Q4pTZ5SYo5LZWOZiiZw9XRfE"; //李晓江
					//userToken =  "oOd-Q4loVo0SEpPlC0me2Q7I3KAs";//段德龙
					//userToken =  "oOd-Q4mkITBprLvsJ056LXdaMkgQ";//顾启旺
					//userToken = "oOd-Q4okp5J8S2yCBhwPX-dEM5v4";//刘勇
					//userToken =  "oOd-Q4u4Fx2jhcqwr-cnVHuAKfJQ";//刘超
					//userToken =  "oOd-Q4iIOqW7XTkL_bQYZ6ZRID0g";//虞国飞
					
					//userToken =  "oOd-Q4hZnuURfbL2BJM6uXzUry5A"; //叶倩
					
					//userToken = "oOd-Q4q9gBTy-E4FpG4Kv7c1FXG0"//袁志锋
					
					//userToken = "oOd-Q4jQPTXxdEICDmt0HXoWBPeU";//谢作尚
					
					//userToken = "oOd-Q4i8cIFnRVjQnDzFmNnN0nqA";//赵石军
					
					//userToken = "oOd-Q4tYFbJ5Pim_GUrF04rFcuzU";//段德龙
					//userToken = "oOd-Q4oycpDKk2BYU1sCsH-mYXzw";//白雨露
					//userToken = "oOd-Q4lO4s8ns6l7l_9WFLp2VO1A";//王海莉
					//userToken = "oOd-Q4tTsWofDUmT2ljKSfVJSWsE";//胡晓龙
					
					
						
				}
				
				
			
				uni.setStorageSync('WEIXIN_USER_KEY',keyInfo[1].data);
				uni.setStorageSync('USER_TOKEN',userToken);
				commit('setWeixinUserKey',keyInfo[1].data);
				commit('setUserToken',keyInfo[1].data.openid);		
			}
			// #endif
		},
		
		
		
		//获取平台会员信息
		getAppUser : async function ({commit,state}) {

			// #ifdef MP-WEIXIN

			// #endif
			
			// #ifndef MP-WEIXIN
			return await new Promise((resolve, reject) => {
				uni.getUserInfo({
					provider: state.loginProvider,
					success: (infoRes) => {
						uni.setStorageSync('APP_USER_INFO',infoRes.userInfo);
						commit('setAppUserInfo',infoRes.userInfo);
						resolve(infoRes.userInfo);
					},
					fail() {
						console.log('登陆失败');					
					}
				});
			});
			// #endif
			
			
			
		},
		
		//获取平台信息
		getProvider: async function ({commit,state}) {
			return await new Promise((resolve, reject) => {
				uni.getProvider({
					service: 'oauth',
					success: (res) => {
						if (res.provider && res.provider.length) {
							commit('setProvider', res.provider[0]);
							resolve(res.provider[0]);
						}
					},
					fail: (err) => {
						console.log('获取服务供应商失败：' + JSON.stringify(err));
						reject(err);
					}
				});
				
			})
		},

	}
}


